#!/bin/sh

set -e

if [ -n "$1" ];then
	APPLICATION=$1
else
	APPLICATION=ubuntu-tweak
fi

if [ -n "$2" ];then
	VERSION=$2
else
	VERSION=0.4.7
fi

if [ -n "$3" ];then
	DISTRO=$3
else
	DISTRO=hardy
fi

if [ -n "$4" ];then
	MODE=binary
else
	MODE=source
fi

VERSION=$2
DISTRO=$3
MKLOG=$PWD/mkchangelog

SRC_ROOT=$HOME/Sources/$APPLICATION
BUILD_DIR=$PWD
BUILD_ROOT=$BUILD_DIR/$APPLICATION

CLEAN_OLD() {
	rm -rf $BUILD_ROOT
}

CLEAN_OLD

CLEAN_SRC() {
	rm -rf $BUILD_ROOT/.git
	rm -rf $BUILD_ROOT/.gitignore
	cd $BUILD_ROOT
	find -name '*.pyc' | xargs rm -rf
	rm -rf build
}

INFO() {
	echo $APPLICATION
	echo $VERSION
	echo $DISTRO
	echo $SRC_ROOT
	echo $BUILD_ROOT
}
INFO

if [ ! -d $BUILD_DIR ];then
	mkdir $BUILD_DIR
fi

cd $SRC_ROOT
#git pull

cp -r $SRC_ROOT $BUILD_ROOT/
$MKLOG $APPLICATION $VERSION $DISTRO $BUILD_ROOT/debian/changelog daily
CLEAN_SRC

cd $BUILD_ROOT

if [ "$MODE" = "binary" ]; then
	debuild
else
	debuild -S -sa
fi
